<%--***************************************
*
*PayPal
* 
***************************************--%>
<%@ page import="
	java.util.*,
	java.math.BigDecimal,
	org.apache.commons.lang.StringUtils,
	com.probiz.estore.Constants,
	com.probiz.estore.system.util.TranslateService,
	com.probiz.estore.order.model.OnlinePaymentResponse,
	com.probiz.estore.common.model.system.PaymentGateway,
	com.probiz.estore.system.service.PaymentGatewayManager,
	com.probiz.estore.common.service.OrderService,
	org.springframework.web.context.support.WebApplicationContextUtils,
	org.springframework.web.context.WebApplicationContext"
	pageEncoding="UTF-8"%>
<%
	response.setCharacterEncoding("UTF-8");
	WebApplicationContext webAppContext = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
	PaymentGatewayManager paymentGatewayManager = (PaymentGatewayManager) webAppContext.getBean("paymentGatewayManager");
	PaymentGateway paymentGateway = paymentGatewayManager.getByPaymentGatewayCode("paypal");
	
	String orderNo=request.getParameter("custom");  //store the order no in custom parameter when commit payment reuqest
	String mc_gross = request.getParameter("mc_gross");				//支付金额
	String txn_id = request.getParameter("txn_id");					//流水号
	String receiver_email = request.getParameter("receiver_email"); //支付人邮件
	BigDecimal total_fee = null;
	if (StringUtils.isNotBlank(mc_gross)) {
		total_fee = new BigDecimal(mc_gross);				
	}
	String type=paymentGateway.getConfigData().get("type").toString();
	String requestUrl="";			//提交地址
	if(type.equals("1")){
		requestUrl="https://www.paypal.com/cgi-bin/webscr";
	}else{
		requestUrl="https://www.sandbox.paypal.com/cgi-bin/webscr";
	}
	
	String payment_status=request.getParameter("payment_status");
	Short flag = Constants.PAYMENT_HISTORY_FLAG_FAILURE;

	if(payment_status!=null && payment_status.equalsIgnoreCase("Completed")){
		Map dataMap=request.getParameterMap();
		Map sendData=new HashMap();
		Iterator i=dataMap.entrySet().iterator();
		while(i.hasNext()){
			Map.Entry en=(Map.Entry)i.next();
			sendData.put(en.getKey(),((String[])en.getValue())[0]);
		}			
		sendData.put("cmd","_notify-validate");
		String responseText=TranslateService.getResponseFromHttpsUrl(requestUrl,sendData);

		if(responseText.trim().equalsIgnoreCase("VERIFIED")){
			//the data come from paypal
			flag = Constants.PAYMENT_HISTORY_FLAG_SUCCESS;
		}else if(responseText.trim().equalsIgnoreCase("INVALID")){
			//Declined (invalid request)
			flag = Constants.PAYMENT_HISTORY_FLAG_FAILURE;
		}else if("Pending".equals(payment_status)){
			//Queued
			flag = Constants.PAYMENT_HISTORY_FLAG_WAIT;
		}
		
		OnlinePaymentResponse onlinePaymentResponse = new OnlinePaymentResponse();
		onlinePaymentResponse.setFlowNo(txn_id);   //必填. 如果支付网关没有流水号，则使用原始的orderNo，即未decode前的
		onlinePaymentResponse.setOrderNo(orderNo);  //必填
		onlinePaymentResponse.setFlag(flag);    //必填
		onlinePaymentResponse.setAmount(total_fee);  //必填
		onlinePaymentResponse.setPaymentGateway(paymentGateway);  //必填
		onlinePaymentResponse.setPayer(receiver_email);
		OrderService orderService = (OrderService) webAppContext.getBean("orderService");
		request.setAttribute("onlinePaymentResponse",onlinePaymentResponse);
		orderService.doHandleOnlinePaymentResponse(onlinePaymentResponse,request);
	}	
%>
<%@include file="./include/finishPayResult.jspf"%>
